//
//  ShellOrderView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/2.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellOrderView.h"
#import "ShellOrderCell.h"
#import "ShellOrderViewModel.h"
#import "LZBaseTableView.h"
#import "UITableViewCell+SectionCorner.h"
#import "ShellOrderDetailViewController.h"
#import "ShellOrderModel.h"

@interface  ShellOrderView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellOrderViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;


@end

@implementation ShellOrderView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellOrderViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
        
    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH, 15)];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH, 15)];

    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.tableView.clipsToBounds = YES;
    self.tableView.cornerRadius = 10;
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self reloadRecordData:[x integerValue]];
        
    }];
    
    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{
        @strongify(self);

        [self.viewModel getOrderList:NO];

    }];
    
    
    [self.tableView addHeadWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getOrderList:YES];

    }];
    
    [self.viewModel getOrderList:YES];
}

//刷新数据
- (void)reloadRecordData:(NSInteger)type{
    
    //定时器刷新
    [kCountDownManager reload];

    self.tableView.mj_header.state = MJRefreshStateIdle;
    self.tableView.mj_footer.state = MJRefreshStateIdle;
    [self.tableView reloadData];
    [self.tableView dismissEmptyView];
    
    switch (type) {
        case 0:{
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
            break;
        default:
            break;
    }
    if(self.viewModel.dataArray.count == 0){
        self.tableView.mj_footer.hidden = YES;
        [self.tableView tableViewDisplayWithImgName:@"pic_nodata" message:LZLocalizedString(@"暂无数据") contentoffsetY:109 showEmptyView:YES];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellOrderCell *orderCell = [tableView dequeueReusableCellWithIdentifier:@"ShellOrderCell"];
    if(self.viewModel.dataArray.count > indexPath.row){
           orderCell.modelObject = self.viewModel.dataArray[indexPath.row];
           if(self.viewModel.dataArray.count - 1 == indexPath.row){
               orderCell.lineView.hidden = YES;
           } else {
               orderCell.lineView.hidden = NO;
           }
           
           if(self.viewModel.dataArray.count == 1){
               [orderCell addSectionCornerViewframe:CGRectMake(15, 0, K_SCREENWIDTH - 30, 90) cornerRadius:10];
           } else {
               [orderCell addSectionCornerWithTableView:tableView indexPath:indexPath cornerViewframe:CGRectMake(15, 0, K_SCREENWIDTH - 30, 90) cornerRadius:10];
           }
       }
    return orderCell;
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(self.viewModel.dataArray.count > indexPath.row){
        ShellOrderModel *orderModel = self.viewModel.dataArray[indexPath.row];
        if([orderModel.status isEqualToString:@"40"])return;
        ShellOrderDetailViewController *detailVC = [[ShellOrderDetailViewController alloc] init];
        detailVC.orderModel = orderModel;
        [LZTool.currentViewController.navigationController pushViewController:detailVC animated:YES];
    }
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 90;
        [_tableView registerClass:[ShellOrderCell class] forCellReuseIdentifier:@"ShellOrderCell"];

    }
    return _tableView;
}
@end
